package com.Ruier.service.imp;

import com.Ruier.dao.HeroDao;
import com.Ruier.pojo.Hero;
import com.Ruier.pojo.PageBean;
import com.Ruier.pojo.query.HeroQuery;
import com.Ruier.service.HeroService;
import com.Ruier.utils.GetNowTime;
import com.Ruier.utils.HandleHeroPosition;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class HeroServiceImp implements HeroService {
    @Autowired
    private HeroDao heroDao;
    @Autowired
    private HandleHeroPosition handleHeroPosition;
    @Autowired
    private GetNowTime nowTime;


    @Override
    public boolean addHero(Hero hero){
        LambdaQueryWrapper<HeroQuery> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HeroQuery::getHeroName,hero.getHeroName());
        HeroQuery heroQuery = heroDao.selectOne(wrapper);
        if(null!=heroQuery){
            return false;
        }
        HeroQuery newhero= new HeroQuery();
        newhero.setHeroName(hero.getHeroName());
        newhero.setHeroImageUrl(hero.getHeroImageUrl());
//        设置添加时间
        newhero.setHeroAddtime(nowTime.getNowTime());
//        处理英雄定位
        String heroPosition = handleHeroPosition.handle(hero.getHeroPosition());
        newhero.setHeroPosition(heroPosition);
        heroDao.insert(newhero);
        return true;
    }
    @Override
    public PageBean<Hero> selectByPage(int currentPage, int pageSize) {
        IPage page=new Page(currentPage,pageSize);
        heroDao.selectPage(page, null);
        PageBean<Hero> pageBean=new PageBean<>();
        pageBean.setRows(page.getRecords());
        pageBean.setTotalCount(page.getTotal());
        return pageBean;
    }

    @Override
    public Hero reshowHero(Long heroId) {
        LambdaQueryWrapper<HeroQuery> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HeroQuery::getHeroId,heroId);
        HeroQuery heroQuery = heroDao.selectOne(wrapper);
        Hero hero=new Hero();
        hero.setHeroId(heroQuery.getHeroId());
        hero.setHeroName(heroQuery.getHeroName());
        hero.setHeroImageUrl(heroQuery.getHeroImageUrl());
        hero.setHeroPosition(handleHeroPosition.handlePositinToInt(heroQuery.getHeroPosition()));
        return hero;
    }

    @Override
    public void updateHero(Hero hero) {
        //查找原来的信息
        LambdaQueryWrapper<HeroQuery> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HeroQuery::getHeroId,hero.getHeroId());
        HeroQuery hq = heroDao.selectOne(wrapper);
        //对比原来的信息判断是否发生修改
        //如果没有修改直接返回
        if(hq.getHeroName().equals(hero.getHeroName())&&
                Arrays.equals(handleHeroPosition.handlePositinToInt(hq.getHeroPosition()),hero.getHeroPosition())&&
                    hq.getHeroImageUrl().equals(hero.getHeroImageUrl())){
            return;
        }
        //修改后更新
        HeroQuery heroQuery=new HeroQuery();
        heroQuery.setHeroId(hero.getHeroId());
        heroQuery.setHeroName(hero.getHeroName());
        heroQuery.setHeroImageUrl(hero.getHeroImageUrl());
        //处理英雄职业和操作时间
        heroQuery.setHeroAddtime(nowTime.getNowTime());
        heroQuery.setHeroPosition(handleHeroPosition.handle(hero.getHeroPosition()));
        heroDao.updateById(heroQuery);
    }

    @Override
    public void deleteHero(Long heroId) {
        LambdaQueryWrapper<HeroQuery> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HeroQuery::getHeroId,heroId);
        heroDao.delete(wrapper);
    }

    @Override
    public void deleteHeroByIds(List<HeroQuery> selectedHero) {
        //取出英雄id进行批量删除
        List<Long> ids=new ArrayList<>();
        for (int i = 0; i < selectedHero.size(); i++) {
            ids.add(selectedHero.get(i).getHeroId());
        }
        heroDao.deleteBatchIds(ids);
    }

    @Override
    public PageBean<HeroQuery> searchHeroes(Hero CondititionHero) {
        String HeroPosition=handleHeroPosition.handle(CondititionHero.getHeroPosition());
        LambdaQueryWrapper<HeroQuery> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(CondititionHero.getHeroName()),HeroQuery::getHeroName,CondititionHero.getHeroName())
                .like(StringUtils.hasLength(HeroPosition),HeroQuery::getHeroPosition,HeroPosition);
        List<HeroQuery> heroQueryList = heroDao.selectList(wrapper);
        PageBean<HeroQuery> pageBean=new PageBean<>();
        pageBean.setRows(heroQueryList);
        int size = heroQueryList.size();
        long totalCount=size;
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }
}
